package com.ajavaer.framework.config;

import com.ajavaer.framework.AjavaerCache;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Configuration
@Component("CacheConfig")
@ConfigurationProperties(prefix = "ajavaer.cache")
public class CacheConfig implements InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;
    @NestedConfigurationProperty
    private List<CacheEngineConfig> engines;

    public List<CacheEngineConfig> getEngines() {
        return engines;
    }

    public void setEngines(List<CacheEngineConfig> engines) {
        this.engines = engines;
    }

    @Override
    public void afterPropertiesSet() {
        AjavaerCache.ME.init(this, applicationContext);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
